package com.cpos.consumerserver.controller;

import com.cpos.consumerserver.util.CposResult;
import com.cpos.consumerserver.util.ErrorResult;
import com.cpos.orderserver.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人 Leiwenlong
 * @创建时间 2020/11/5 0005
 * @描述
 **/
@RestController
public class ConsumerContrller {

    Logger logger = LoggerFactory.getLogger(ConsumerContrller.class);

    //这里注入的restTemplate就是在com.cpos.ConsumerServerApplication中通过@Bean配置的实例
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/consumer-service")
    public String helloConsumer() {
        //调用order-service服务，注意这里用的是服务名，而不是具体的ip+port
        String template = restTemplate.getForObject("http://order-service/buy", String.class);
        logger.info(template);
        return template;
    }

    @RequestMapping("/order/{goodId}")
    public CposResult order(@PathVariable("goodId") String goodId){
        List<Map<Object,Object>> resultList = new ArrayList<>();
        CposResult result = new CposResult();
        try {
            List<Map<Object, Object>> maps = orderService.OrderToStock(goodId);
            result.setMsg("订单存入库存成功");
            result.setData(maps);
        } catch (Exception e) {
            e.printStackTrace();
            return ErrorResult.getError();
        }
        return result;
    }

}
